package cn.web.tunnelrobot.service.impl;

import cn.web.tunnelrobot.dao.*;
import cn.web.tunnelrobot.dto.WorkArea1;
import cn.web.tunnelrobot.dto.WorkAreaDTO;
import cn.web.tunnelrobot.pojo.*;
import cn.web.tunnelrobot.service.WorkAreaService;
import cn.web.tunnelrobot.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class WorkAreaServiceImpl implements WorkAreaService {
    @Autowired
    private WorkAreaDao workAreaDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserRecordDao userRecordDao;
    @Autowired
    private UnitInfoDao unitInfoDao;
    @Autowired
    private ProjectDao projectDao;
    @Override
    public HashMap<String, Object> addWorkArea(WorkArea1 workArea) {
        HashMap<String,Object> map = new HashMap<String,Object>();
/*        if(Constants.is_intranet == 1){
            //内网不提供编辑权限
            map.put("code",-1);
            map.put("msg","内网没有权限");
            return map;
        }*/
        if (workArea.getOperator() == null){
            map.put("code",-1);
            map.put("msg","操作人不能为空");
            return map;
        }
        User user = new User();
        user.setUsId(workArea.getOperator());
        User user1 = userDao.getUserInfo(user);
        if (user1.getUsRole() == 2){
            map.put("code",-1);
            map.put("msg","暂无权限");
            return map;
        }
        workArea.setWaCreateDate(new Date());
        workArea.setWaCreateBy(user.getUsId());
        WorkArea workArea1 = workAreaDao.getWorkAreaByUsId(workArea.getUsId());
        if (workArea1 != null){
            map.put("code",-1);
            map.put("msg","该用户已有负责标段");
            return map;
        }
        UnitInfo unitInfo = unitInfoDao.getUnitInfoByUsId(workArea.getUsId());
        if (unitInfo != null){
            map.put("code",-1);
            map.put("msg","该用户已有负责施工单位");
            return map;
        }
        Project project = projectDao.getProjectByUsId(workArea.getUsId());
        if (project != null){
            map.put("code",-1);
            map.put("msg","该用户已有负责项目");
            return map;
        }
        WorkArea project1 = workAreaDao.getWorkAreaByName(workArea.getPrId(),workArea.getWaName());
        if (project1 != null){
            map.put("code",-1);
            map.put("msg","标段名已存在，请勿重复添加");
            return map;
        }
        int result = workAreaDao.addWorkArea(workArea);
        if (result == 1){
            map.put("msg","添加成功");
            map.put("code",0);
            /*HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            User user2 = (User) request.getSession().getAttribute(Constants.USER_SESSION);
            UserRecord userRecord = new UserRecord();
            userRecord.setUsId(user2.getUsId());
            userRecord.setUrOperatContent("添加标段信息");
            userRecord.setUrOperatTime(new Date());
            userRecordDao.addUserRecord(userRecord);*/
        }else {
            map.put("code",-1);
            map.put("msg","添加失败");
        }
        return map;
    }
    @Override
    public HashMap<String, Object> updateWorkArea(WorkArea1 workArea) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        if(Constants.is_intranet == 1){
            //内网不提供编辑权限
            map.put("code",-1);
            map.put("msg","内网没有权限");
            return map;
        }
        User user = new User();
        user.setUsId(workArea.getOperator());
        User user1 = userDao.getUserInfo(user);
        if (user1.getUsRole() == 2){
            map.put("code",-1);
            map.put("msg","暂无权限");
            return map;
        }
        workArea.setPrId(null);
        WorkArea workArea1 =  workAreaDao.getWorkAreaById(workArea.getWaId());
        if (workArea.getUsId() != workArea1.getUsId()){
            WorkArea workArea3 = workAreaDao.getWorkAreaByUsId(workArea.getUsId());
            if (workArea3 != null){
                map.put("code",-1);
                map.put("msg","该用户已有负责标段");
                return map;
            }
            UnitInfo unitInfo1 = unitInfoDao.getUnitInfoByUsId(workArea.getUsId());
            if (unitInfo1 != null){
                map.put("code",-1);
                map.put("msg","该用户已有负责施工单位");
                return map;
            }
            Project project2 = projectDao.getProjectByUsId(workArea.getUsId());
            if (project2 != null){
                map.put("code",-1);
                map.put("msg","该用户已有负责项目");
                return map;
            }
        }
        if (!workArea1.getWaName().equals(workArea.getWaName())){
            WorkArea workArea2 = workAreaDao.getWorkAreaByName(workArea1.getPrId(),workArea.getWaName());
            if (workArea2 != null){
                map.put("msg","标段名已存在，请勿重复提交");
                map.put("code",-1);
                return map;
            }
        }
        int result = workAreaDao.updateWorkArea(workArea);
        if (result == 1){
            map.put("msg","修改成功");
            map.put("code",0);
            /*HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            User user2 = (User) request.getSession().getAttribute(Constants.USER_SESSION);
            UserRecord userRecord = new UserRecord();
            userRecord.setUsId(user2.getUsId());
            userRecord.setUrOperatContent("修改标段信息");
            userRecord.setUrOperatTime(new Date());
            userRecordDao.addUserRecord(userRecord);*/
        }else {
            map.put("code",-1);
            map.put("msg","修改失败");
        }
        return map;
    }
    @Override
    public HashMap<String, Object> getWorkAreaList(Integer prId) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        List<WorkArea> list = new ArrayList<>();
        try {
            list = workAreaDao.getWorkAreaList(prId);
            if (list.size() > 0){
                map.put("code",0);
                map.put("data",list);
               /* HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
                User user2 = (User) request.getSession().getAttribute(Constants.USER_SESSION);
                UserRecord userRecord = new UserRecord();
                userRecord.setUsId(user2.getUsId());
                userRecord.setUrOperatContent("查询标段列表");
                userRecord.setUrOperatTime(new Date());
                userRecordDao.addUserRecord(userRecord);*/
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }
    @Override
    public HashMap<String, Object> getWorkAreaByWaId(Integer waId) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        WorkAreaDTO workAreaById = workAreaDao.getDetailWorkAreaById(waId);
        if (workAreaById != null){
            map.put("code",0);
            map.put("data",workAreaById);
            /*HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            User user2 = (User) request.getSession().getAttribute(Constants.USER_SESSION);
            UserRecord userRecord = new UserRecord();
            userRecord.setUsId(user2.getUsId());
            userRecord.setUrOperatContent("查询标段详情");
            userRecord.setUrOperatTime(new Date());
            userRecordDao.addUserRecord(userRecord);*/
        }else {
            map.put("code",-1);
            map.put("msg","删除失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> deleteWorkAreaByWaId(Integer waId){
        HashMap<String, Object> map=new HashMap<>();
        int result=workAreaDao.deleteWorkAreaByWaId(waId);
        if(result==1){
            map.put("code",0);
            map.put("msg","删除成功");
        }else {
            map.put("code",-1);
            map.put("msg","删除失败");
        }
        return map;
    }
}
